import Mock from "mockjs";
// 订单接口模拟数据
var orderlist = {}
const getimage = () => {
    const Arrimg = [
        'https://gw.alicdn.com/bao/uploaded/i4/2209530298329/O1CN01ilmkXR2BOjbiqK0pH_!!0-item_pic.jpg_300x300q90.jpg_.webp',
        '//gw.alicdn.com/bao/uploaded/i3/2104833053/O1CN01so2GAu1YQJp2eoE7M_!!0-item_pic.jpg_300x300q90.jpg_.webp',
        '//gw.alicdn.com/bao/uploaded/i1/116946681/O1CN01891LHp1zDwtdbay39_!!0-saturn_solar.jpg_300x300q90.jpg_.webp'
    ]
    const index = Mock.Random.integer(0, Arrimg.length - 1)
    return Arrimg[index]
}
if (localStorage.getItem('order_list')) {
    orderlist = JSON.parse(localStorage.getItem("order_list"))
} else {
    orderlist = Mock.mock({
        "list|100": [
            {
                'orderid|000000-999999': 1,
                img: getimage(),
                time: '@date(MM-dd)',
                name: '@cname',
                'phone|13000000000-19999999999': 1,
                address: '@county(true)',
                status: "@pick(['已收货', '未收货'])",
                ordertype: "@pick(['会员订单', '普通订单'])",
                pay: "@pick(['支付宝', '微信','银行卡','花呗'])",
                deal: '@id',
                'water|2022000000-2022999999': 1,
                "good|1-3": [
                    {
                        goodname: "@pick(['连衣裙','卫衣','秋季外套','男士上衣','保暖秋裤','羽绒服'])",
                        'price|80-500': 1,
                        size: "@pick(['S','M','L','XL','XXL'])",
                        'num|1-4': 1
                    }
                ]
            }
        ]
    })
    localStorage.setItem('order_list', JSON.stringify(orderlist))
}
//订单列表
Mock.mock('/api/order/list', 'get', () => {
    return orderlist
})
// 删除订单
Mock.mock('/api/order/dellist', 'post', (newitem) => {
    var id = JSON.parse(newitem.body)
    console.log(newitem.body)
    var del = orderlist.list.findIndex((item) => {
        return item.orderid == id
    })
    if (del != -1) {
        orderlist.list.splice(del, 1)
        localStorage.setItem('order_list', JSON.stringify(orderlist))
    }
    return { status: 200, msg: '删除成功!', list: orderlist.list }
})
//订单列表
Mock.mock('/api/order/detail', 'post', (detail) => {
    var goodid = JSON.parse(detail.body)
    var tail = orderlist.list.findIndex((item) => {
        return item.orderid == goodid
    })
    if (tail != -1) {
        return { list: orderlist.list[tail] }
    }
})
//修改订单
Mock.mock('/api/order/edit', 'post', (req) => {
    var editorder = JSON.parse(req.body)
    var id = editorder.orderid
    var index = orderlist.list.findIndex((item) => {
        return item.orderid == id
    })
    console.log(index)
    if (orderlist.list[index].name == editorder.name && orderlist.list[index].phone == editorder.phone && orderlist.list[index].address == editorder.address && orderlist.list[index].status == editorder.status) {
        console.log(111)
        return { code: '304', status: 'no', msg: '与原订单一致' }
    } else {
        orderlist.list[index].name = editorder.name
        orderlist.list[index].phone = editorder.phone
        orderlist.list[index].address = editorder.address
        orderlist.list[index].status = editorder.status
        localStorage.setItem('order_list', JSON.stringify(orderlist))
        return {
            code: '200',
            status: 'ok',
            list: orderlist.list
        }
    }
})
// 查询订单
Mock.mock('/api/order/search', 'post', (req) => {
    var id = JSON.parse(req.body)
    var index = orderlist.list.findIndex((item) => {
        return item.orderid == id
    })
    // neworder = Array.from(JSON.parse(orderlist.list[index]))
    if (index != -1) {
        var neworder = []
        neworder.push(orderlist.list[index])
        // console.log(neworder)neworder
        return { code: '200', status: 'ok', list: neworder }

    } else {
        return { code: '400', status: 'no' }
    }
})
// 订单详情
Mock.mock('/api/order/good', 'post', (req) => {
    var id = JSON.parse(req.body)
    var index = orderlist.list.findIndex((item) => {
        return item.orderid == id
    })
    var a = orderlist.list[index].good
    console.log(index)
    if (index != -1) {
        return { code: 200, status: 'ok', list: a }
    }
})